#Boa:Frame:Frame1

import wx
import re
import wx.lib.colourselect as  csel

DieWidth=10

Bins={}
FlagStartX=0
FlagEndX=0
FlagStartY=0
FlagEndY=0
BinColours={'1':(  0,255,  0),
            '2':(255,  0,  0),
            '3':(200, 60,180),  
            '4':(  0,  0,255), 
            '5':(  0,  0,  0), 
            '6':(255,100,130),
            '7':(255,128,  0), 
            '8':(255,255,  0), 
            '9':(128,  0,  0), 
            '10':(127,  0,255),
            '11':(128,128,  0),
            '12':(255,128,  0), 
            '13':(128,128,255),
            '14':(128,  0,128),  
            '15':(127,  0,255), 
            '16':(  0,255,255), 
            '17':(255,  0,255), 
            '18':(230,255,230), 
            '19':(200,204, 51),
            '20':( 70,165,185), 
            '21':(185,185,170),
            '22':(160, 95,146)
            }


def ReadBHTCData():
    BHTCFileFH=open("temp.bhtc","r") 
    global Bins
    global FlagStartX
    global FlagEndX
    global FlagStartY
    global FlagEndY
    for Line in BHTCFileFH.readlines():
        if re.search(r"((\s+){0,}(-){0,1}\d+\|){4,}",Line,re.I):
            Temp=re.split(r"\|",Line)
            TempX=int(Temp[2])
            TempY=int(Temp[3])
            if FlagStartX>TempX:
                FlagStartX=TempX
            if FlagEndX<TempX:
                FlagEndX=TempX
            if FlagStartY>TempY:
                FlagStartY=TempY
            if FlagEndY<TempY:
                FlagEndY=TempY
            TempKey="".join(("X",str(TempX),"Y",str(TempY)))
            Bins[TempKey]=Temp[1].strip()
class Canve(wx.Panel):    
    def __init__(self,parent):
        wx.Panel.__init__(self,parent,-1 )
        self.SetBackgroundColour('white')
        #self.SetSize((XSizer,YSizer))
        #self.SetAutoLayout(True)
        self.Bind(wx.EVT_PAINT,self.Draw)
        self.Bind(wx.EVT_MOTION,self.OnMotion)
    def Draw(self,event):
        dc=wx.BufferedPaintDC(self)
        for Indey in range(0,FlagEndY-FlagStartY+5):
            for Index in range(0,FlagEndX-FlagStartX+5):
                TempKey="".join(("X",str(Index+FlagStartX-2),"Y",str(Indey+FlagStartY-2)))
                if Bins.has_key(TempKey):
                    BrushColour=BinColours[Bins[TempKey]]
                    dc.SetPen(wx.Pen(wx.BLACK))
                    dc.SetBrush(wx.Brush(BrushColour))
                    dc.DrawRectangle(DieWidth*(Index),DieWidth*(Indey),DieWidth,DieWidth)
                else:  
                    dc.SetPen(wx.Pen(wx.WHITE))
                    dc.SetBrush(wx.Brush(wx.WHITE))
                    dc.DrawRectangle(DieWidth*(Index),DieWidth*(Indey),DieWidth,DieWidth)
    def OnMotion(self,event):
        CoordX=event.GetX()/DieWidth+FlagStartX-2
        CoordY=event.GetY()/DieWidth+FlagStartY-2
        TempKey="".join(("X",str(CoordX),"Y",str(CoordY)))
        if Bins.has_key(TempKey):
            print ("Bin:",Bins[TempKey],"; Coord:","("+str(CoordX)+","+str(CoordY)+")")
class BinColourSet(wx.Panel):
    def __init__(self,parent):
        global BinColours
        wx.Panel.__init__(self, parent, -1)
        self.SetAutoLayout(True)
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(mainSizer)
        buttonData = [
            ("PASS",   (0, 255, 0),   wx.DefaultSize, ""),
            ("BIN 2",  (255, 0, 0),   wx.DefaultSize, ""),
            ("BIN 3",  (200, 60, 180),     wx.DefaultSize, ""),
            ("BIN 4",  (0, 0, 255),     wx.DefaultSize, ""),
            ("BIN 5",  (0, 0 ,  0),   wx.DefaultSize, ""),
            ("BIN 6",  (255, 100, 130), wx.DefaultSize, ""),
            ("BIN 7",  (255, 128, 0),   wx.DefaultSize, ""),
            ("BIN 8",  (255, 255, 0),     wx.DefaultSize, ""),
            ("BIN 9",  (128, 0, 0),     wx.DefaultSize, ""),
            ("BIN 10",  (127, 0, 255),   wx.DefaultSize, ""),
            ("BIN 11",  (128, 128, 0), wx.DefaultSize, ""),
            ("BIN 12",  (255, 128, 0),   wx.DefaultSize, ""),
            ("BIN 13",  (128, 128, 255),     wx.DefaultSize, ""),
            ("BIN 14",  (128, 0, 128),     wx.DefaultSize, ""),
            ("BIN 15",  (127, 0, 255),   wx.DefaultSize, ""),
            ("BIN 16",   (0, 255, 255), wx.DefaultSize, ""),
            ("BIN 17",  (255, 0, 255),   wx.DefaultSize, ""),
            ("BIN 18",  (230, 255, 230),     wx.DefaultSize, ""),
            ("BIN 19",  (200, 204, 51),    wx.DefaultSize, ""),
            ("BIN 20",  (70, 165, 185),   wx.DefaultSize, ""),
            ("BIN 21",  (185, 185, 170), wx.DefaultSize, ""),
            ("BIN 22",  (160, 95, 146),   wx.DefaultSize, ""),
            ]

        self.buttonRefs = [] # for saving references to buttons
        buttonSizer = wx.FlexGridSizer(6, 4) # sizer to contain all the example buttons
        # build each button and save a reference to it
        for name, color, size, label in buttonData:
            b = csel.ColourSelect(self, -1, label, color, size = size)
            b.Bind(csel.EVT_COLOURSELECT, self.OnSelectColour)
            self.buttonRefs.append((name, b))  # store reference to button
            buttonSizer.AddMany([
                (wx.StaticText(self, -1, name), 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL),
                (b, 0, wx.ALL, 3),
                ])
        mainSizer.Add(buttonSizer, 0, wx.ALL, 3)
        self.Layout()
    def OnSelectColour(self,event):
        print("name :%s Colour selected: %s" % (name, str(event.GetValue())))
class MainFrame(wx.Frame):
    def __init__(self,parent,title):
        MyFrame=wx.Frame.__init__(self,parent,-1,title,)
        #self.SetAutoLayout(True)
        global Bins
        global FlagStartX
        global FlagEndX
        global FlagStartY
        global FlagEndY
        global BinColours
        global DieWidth
        
        ReadBHTCData()
        if (FlagEndY-FlagStartY)<60 or (FlagEndX-FlagStartX)<60:
            DieWidth=7
        elif (FlagEndY-FlagStartY)<50 or (FlagEndX-FlagStartX)<50:
            DieWidth=10
        elif (FlagEndY-FlagStartY)<40 or (FlagEndX-FlagStartX)<40:
            DieWidth=15
        elif (FlagEndY-FlagStartY)<30 or (FlagEndX-FlagStartX)<30:
            DieWidth=20
        elif (FlagEndY-FlagStartY)<20 or (FlagEndX-FlagStartX)<20:
            DieWidth=25    
        else:
            DieWidth=5  
        YSizer=(FlagEndY-FlagStartY+10)*DieWidth
        XSizer=(FlagEndX-FlagStartX+6)*DieWidth
        self.SetSizeWH(XSizer,YSizer)
        #BinColourSet(self)
        self.Canve=Canve(self)
if __name__=='__main__':
    app=wx.PySimpleApp()
    frame=MainFrame(None,u"WaferMap View")
    frame.Show(True)
    app.MainLoop()